package com.example.controller.buyer;

import com.example.entity.SpuVo;
import com.example.entity.enums.UserPreferEnum;
import com.example.service.impl.UserPreferService;
import com.example.vo.IdHolder;
import com.example.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 朽
 */
@RestController
@Slf4j
@RequestMapping("/buyer/recommend")
public class UserPreferController {

    @Autowired
    private UserPreferService userPreferService;

    @GetMapping("/spulist")
    @Operation(summary = "获取推荐商品列表的接口")
    public Result<List<SpuVo>> getRecommendSpuList(@RequestParam(value = "count",defaultValue = "6") int count) {
        return Result.ok(userPreferService.getUserPreferSpuList(count));
    }

    @PostMapping("/record")
    @Operation(summary = "记录用户偏好的接口")
    public Result<String> recordUserPrefer(@RequestParam("goodsId") Long goodsId, @RequestParam("type") int typeValue) {
        UserPreferEnum type = UserPreferEnum.getTypeByValue(typeValue);
        if (type == null) {
            return Result.error("无效的用户偏好");
        }
        Long userId = IdHolder.getUserId();
        userPreferService.recordUserScore(userId, goodsId, type);
        return Result.ok("用户偏好设置成功");
    }
}